﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Housing;

public partial class ManageCenter_ManageUsers : System.Web.UI.Page
{
    Users user = new Users();
    public string url;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserId"] != null)
            {
                PageBind();
                ddlpages.Items.Clear();
                for (int i = 1; i <= gvshowusers.PageCount; i++)
                {
                    ddlpages.Items.Add("第" + i + "页");
                }
            }
            else
                WebHelper.AlertAndRedirect("登陆超时，请重新登录","../Login.aspx");
        }
    }
    protected void PageBind()
    {
        DataSet ds=user.GetAllUsers(Session["UserId"].ToString());
        gvshowusers.DataSource=ds;
        gvshowusers.DataBind();
        if (gvshowusers.PageIndex == 0 && gvshowusers.PageCount == 1)
        {
            btnfirstpage.Enabled = false;
            btnlastpage.Enabled = false;
            btnnextpage.Enabled = false;
            btnprepage.Enabled = false;
        }
        else if (gvshowusers.PageIndex == 0 && gvshowusers.PageCount>1)
        {
            btnfirstpage.Enabled = false;
            btnlastpage.Enabled = true;
            btnnextpage.Enabled = true;
            btnprepage.Enabled = false;
        }
        else if (gvshowusers.PageIndex >0 && gvshowusers.PageIndex< gvshowusers.PageCount - 1)
        {
            btnfirstpage.Enabled = true;
            btnlastpage.Enabled = true;
            btnnextpage.Enabled = true;
            btnprepage.Enabled = true;
        }
        else if (gvshowusers.PageIndex == gvshowusers.PageCount - 1)
        {
            btnfirstpage.Enabled = true;
            btnlastpage.Enabled = false;
            btnnextpage.Enabled = false;
            btnprepage.Enabled = true;
        }
    }
    protected string GetUserTpye(string type,string street)
    {
        if (type == "住房保障管理中心" || type == "民政部门")
            return type;
        else if (type == "辖区房管所")
            return street + type.Substring(2, 3);
        else if (type == "街道办事处")
            return type + "(" + street + ")";
        else
            return null;
    }
    protected void btnnextpage_Click(object sender, EventArgs e)
    {
        int index = gvshowusers.PageIndex;
        gvshowusers.PageIndex = index + 1;
        PageBind();
    }
    protected void btnprepage_Click(object sender, EventArgs e)
    {
        int index = gvshowusers.PageIndex;
        gvshowusers.PageIndex = index - 1;
        PageBind();
    }
    protected void btnfirstpage_Click(object sender, EventArgs e)
    {
        gvshowusers.PageIndex = 0;
        PageBind();
    }
    protected void btnlastpage_Click(object sender, EventArgs e)
    {
        gvshowusers.PageIndex = gvshowusers.PageCount - 1;
        PageBind();
    }
    protected void ddlpages_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = Convert.ToInt32(ddlpages.SelectedItem.ToString().Substring(1, 1));
        gvshowusers.PageIndex = index - 1;
        PageBind();
    }
    protected void gvshowusers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        LinkButton lkbtn = (LinkButton)e.Row.FindControl("lkbtndel");
        if (lkbtn != null)
            lkbtn.Attributes.Add("onclick","return confirm('确定要删除该用户吗？')");
    }
    protected void gvshowusers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "view")
        {
            paneuserinfor.Visible = true;
            int userid = Convert.ToInt32(e.CommandArgument.ToString());
            user.GetUserByUserId(userid);
            if (user!=null)
            {
                txbusername.Text = user.UserName;
                ddltype.SelectedValue = user.UserType;
                ddlstreet.SelectedValue = user.UserStreet;
                txbpwd.Text = user.Password;
                lbluserid.Text = e.CommandArgument.ToString();
            }
            else
                paneuserinfor.Visible = false;
        }
        if (e.CommandName.ToLower() == "del")
        {
            if (user.DeleteAUser(Convert.ToInt32(e.CommandArgument.ToString())))
                PageBind();
            else
                WebHelper.Alert("删除失败，请重新操作！");
        }
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        bool succ = false;
        user = new Users();
        user.UserId = Convert.ToInt32(lbluserid.Text);
        user.UserName = txbusername.Text;
        user.Password = txbpwd.Text;
        user.UserType = ddltype.SelectedValue;
        if (ddltype.SelectedValue == "街道办事处" || ddltype.SelectedValue == "辖区房管所")
        {
            if (ddlstreet.SelectedValue == "海曙")
                WebHelper.Alert("请选择街道");
            else
            {
                user.UserStreet = ddlstreet.SelectedValue;
                succ = user.UpdateAUser(user);
            }
        }
        else
        {
            user.UserStreet = "海曙";
            succ = user.UpdateAUser(user);
        }
        if (succ)
        {
            paneuserinfor.Visible = false;
            WebHelper.Alert("修改成功");
            PageBind();
        }
        //Response.Redirect("ManageUsers.aspx");
        else
            WebHelper.Alert("修改信息失败，请重新操作");
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        url = "AddUser.aspx";
        Response.Write("<script type=\"text/javascript\">window.setTimeout('ss()',100)</script>");
    }
}
